package cn.wolfcode.p2p.mgrsite.web.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.LoginInfoContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
    @Autowired
    private ILoginInfoService loginInfoService;

    // 登录
    @PostMapping("/login")
    @ResponseBody
    public Object login(String username, String password){
        JSONResult result = new JSONResult();
        // 后台登录只能登录管理员
        LoginInfo loginInfo = loginInfoService.login(username, password, LoginInfo.TYPE_MANAGER);
        if (loginInfo == null) {
            result.mark("账号或密码错误");
        }
        return result;
    }
    // 注销
    @RequestMapping("/logout")
    public String logout(String username, String password){
        // 清空session的验证码vo对象
        LoginInfoContext.setCurrentLoginInfo(null);
        return "redirect:/login.html";
    }

    // main
    @RequestMapping("/main")
    public String toMain(){
        return "main";
    }
}
